define(function(require) {
	var $ = require("jquery");
	var justep = require("$UI/system/lib/justep");
	require("$UI/system/lib/cordova/cordova");
	require("cordova!cordova-plugin-camera");

	var Model = function() {
		this.callParent();
		this.url = "http://yun.jiawoltd.com";
		this.parent = "";
		this.nodeId = "";
		this.imageCount = 1;
	};

	Model.prototype.modelParamsReceive = function(event) {
		if (this.params && this.params.nodeId && this.params.parent) {
			this.nodeId = this.params.nodeId;
			this.parent = this.params.parent;
		}
	};

	Model.prototype.button2Click = function(event){
		
		if(!navigator.camera){
                return;
        }
        
        if(this.imageCount >=4){
        	throw justep.Error.create("现场图片上传不能超过3张");
        }
        var self = this;                                
        navigator.camera.getPicture(onLoadImageSuccess, onLoadImageFail, {
                destinationType:navigator.camera.DestinationType.DATA_URL,
                allowEdit:false,
                quality:90,
                targetWidth:800,
                targetHeight:500,
                saveToPhotoAlbum:true
        });
        
        // 拍照成功后回调
        function onLoadImageSuccess(imageData){
        		var itemKey = "imageData1";
        		var imgId = "image1";
        		if(self.imageCount == 2){
        			itemKey = "imageData2";
        			imgId = "image2"
        		}else if(self.imageCount == 3){
        			itemKey = "imageData3";
        			imgId = "image3";
        		}
                localStorage.setItem(itemKey,"data:image/jpeg;base64,"+imageData);
                var smallImage = document.getElementById(self.getIDByXID(imgId));
                // 显示图像
                smallImage.style.display = 'block';
                smallImage.src = "data:image/jpeg;base64,"+imageData;
                self.imageCount += 1;
                // 图片上传
                //self.uploadPic(imageData);
        }

        function onLoadImageFail(error){
                alert(error);
        }
	};

	Model.prototype.saveScglButClick = function(event){
		var imageData1 = localStorage.getItem("imageData1");
		var imageData2 = localStorage.getItem("imageData2");
		var imageData3 = localStorage.getItem("imageData3");
		var operator = this.comp("operator").val();		
		
		
		$.ajax({
				cache : false,
				type : "POST",
				timeout : 40000, // 超时时间设置，单位毫秒
				url : this.url + "/mobile/addscgl/",
				data : {
					parent : this.parent,
					nodeId : this.nodeId,
					operator : operator,
					imageData1 : imageData1,
					imageData2 : imageData2,
					imageData3 : imageData3
				},
				async : false,
				error : function(request) {
				},
				success : function(resp) {
					localStorage.clear();
					justep.Shell.setIsSinglePage();
					var url = './scgl.w';
					justep.Shell.showPage(require.toUrl(url), {});
				}
			});
		
	};

	return Model;
});